D:\a\tools.proto\tools.proto\compiler\src\gen\swift\core.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::codec_map_initializer; |
30 | | use crate::compiler::util::imports::ProtocolStore; |
31 | | use crate::compiler::Protocol; |
32 | | use crate::gen::base::Error; |
33 | | use crate::gen::codec::CodecMap; |
34 | | use crate::gen::file::{Content, File, FileType}; |
35 | | use crate::gen::swift::imports::gen_imports; |
36 | | use crate::gen::swift::message::{gen_message_decl, gen_message_from_slice_impl, gen_message_write_impl}; |
37 | | use crate::gen::swift::r#enum::gen_enum_decl; |
38 | | use crate::gen::swift::solver::SwiftImportSolver; |
39 | | use crate::gen::swift::structure::gen_structure_decl; |
40 | | use crate::gen::swift::union::gen_union_decl; |
41 | | use crate::gen::Generator; |
42 | | use crate::gen::template::Template; |
43 | | |
44 | | const TEMPLATE_CODEC_BASE: &[u8] = include_bytes!("./default_codec/base.template"); |
45 | | const TEMPLATE_CODEC_STRING: &[u8] = include_bytes!("./default_codec/string.template"); |
46 | | const TEMPLATE_CODEC_LIST: &[u8] = include_bytes!("./default_codec/list.template"); |
47 | | |
48 | | pub struct GeneratorSwift; |
49 | | |
50 | | impl Generator for GeneratorSwift { |
51 | | type Error = Error; |
52 | | type Params<'a> = ProtocolStore<'a, SwiftImportSolver>; |
53 | | |
54 | 0 | fn get_default_codecs<'fragment, 'variable>() -> CodecMap<'fragment, 'variable> { |
55 | 0 | let mut codecs = CodecMap::new(codec_map_initializer! { |
56 | 0 | "base" => TEMPLATE_CODEC_BASE |
57 | 0 | }); |
58 | 0 | codecs.insert("string", Template::compile_with_includes(TEMPLATE_CODEC_STRING, &codecs).unwrap()); |
59 | 0 | codecs.insert("list", Template::compile_with_includes(TEMPLATE_CODEC_LIST, &codecs).unwrap()); |
60 | 0 | codecs |
61 | 0 | } |
62 | | |
63 | 0 | fn generate( |
64 | 0 | proto: &Protocol, |
65 | 0 | codec_map: &CodecMap, |
66 | 0 | params: &ProtocolStore<SwiftImportSolver>, |
67 | 0 | ) -> Result<Vec<File>, Self::Error> { |
68 | 0 | let imports = gen_imports(params); |
69 | 0 | let decl_structures = proto.structs.iter().map(|v| gen_structure_decl(proto, v)); |
70 | 0 | let decl_enums = proto.enums.iter().map(|v| gen_enum_decl(proto, v)); |
71 | 0 | let decl_messages_code = proto.messages.iter().map(|v| gen_message_decl(proto, codec_map, v)); |
72 | 0 | let impl_from_slice_messages_code = |
73 | 0 | proto.messages.iter().map(|v| gen_message_from_slice_impl(proto, codec_map, v)); |
74 | 0 | let impl_write_messages_code = proto.messages.iter().map(|v| gen_message_write_impl(proto, codec_map, v)); |
75 | 0 | let decl_unions = proto.unions.iter().map(|v| gen_union_decl(proto, v)); |
76 | 0 | Ok(vec![ |
77 | 0 | File::new( |
78 | 0 | FileType::Structure, |
79 | 0 | format!("{}.structures.swift", proto.name()), |
80 | 0 | Content::from_iter(decl_structures).header(&imports), |
81 | 0 | ), |
82 | 0 | File::new( |
83 | 0 | FileType::Enum, |
84 | 0 | format!("{}.enums.swift", proto.name()), |
85 | 0 | &Content::from_iter(decl_enums), |
86 | 0 | ), |
87 | 0 | File::new( |
88 | 0 | FileType::Message, |
89 | 0 | format!("{}.messages.swift", proto.name()), |
90 | 0 | Content::try_from_iter(decl_messages_code)?.header(&imports), |
91 | 0 | ), |
92 | 0 | File::new( |
93 | 0 | FileType::MessageWriting, |
94 | 0 | format!("{}.messages_write.swift", proto.name()), |
95 | 0 | Content::try_from_iter(impl_write_messages_code)?.header(&imports), |
96 | 0 | ), |
97 | 0 | File::new( |
98 | 0 | FileType::MessageReading, |
99 | 0 | format!("{}.messages_from_bytes.swift", proto.name()), |
100 | 0 | Content::try_from_iter(impl_from_slice_messages_code)?.header(&imports), |
101 | 0 | ), |
102 | 0 | File::new( |
103 | 0 | FileType::Union, |
104 | 0 | format!("{}.unions.swift", proto.name()), |
105 | 0 | Content::from_iter(decl_unions).header(&imports), |
106 | 0 | ), |
107 | | ]) |
108 | 0 | } |
109 | | |
110 | 0 | fn get_language_extension() -> &'static str { |
111 | 0 | "swift" |
112 | 0 | } |
113 | | } |